/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.database.engine.ddl.table.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.server.database.engine.ddl.table.entity.Table;
import com.snail.server.database.engine.ddl.table.mapper.TableMapper;
import com.snail.starter.commons.exception.BizException;
import com.snail.server.database.engine.error.DatabaseEngineErrorCode;
import org.springframework.stereotype.Service;

/**
 * @author zgadmin
 * @date 2025/8/29 14:44
 */
@Service
public class TableService extends ServiceImpl<TableMapper, Table> {

    public Table checkById(Long dataSourceId, Long tableId) {
        Table table = getOne(Wrappers.lambdaQuery(Table.class).eq(Table::getDataSourceId, dataSourceId).eq(Table::getTableId, tableId));
        if (null == table) {
            throw BizException.of(DatabaseEngineErrorCode.TABLE_NOT_EXISTS, new Object[]{dataSourceId + "-" + tableId});
        }
        return table;
    }

    public boolean dropTable(Long dataSourceId, Long tableId) {
        return remove(Wrappers.lambdaQuery(Table.class).eq(Table::getDataSourceId, dataSourceId).eq(Table::getTableId, tableId));
    }

}